/**
 * 
 */
package util;

import java.sql.*;
import java.util.HashMap;

import data.SimilaritySet;

/**
 * @author yexijiang
 * @date 2008-7-2
 */
public class SetReader 
{

	private int m_SetDimension;		//	the number of sets
	private int m_RecordNum;	//	the number of records to be read
	private int m_SetSequence[];
	private String m_DMUrl = "jdbc:mysql://localhost:3306/dm";
	private String m_TagUrl = "jdbc:mysql://localhost:3306/tag";
	private String m_Usr = "root";
	private String m_Pwd = "";
	
	private HashMap<String, String> m_FrequentTagDict;
	
	SimilaritySet m_Set;
	
	public SetReader(int recordNum, int sequence[])
	{
		m_SetDimension = sequence.length;
		m_RecordNum = recordNum;
		m_FrequentTagDict = new HashMap<String, String>();
		m_SetSequence = sequence;
		//m_Sets = new SimilaritySet[setNum];
	}
	
	/**
	 * Read the sets by the given id of the target tag
	 * @param tagId	the id of the target tag
	 */
	public SimilaritySet readSets(String tagId)
	{

		String targetTagName = getTagNamebyId(tagId);
		
		Connection dmConn = DBConnect.connect(m_DMUrl, m_Usr, m_Pwd);
		Statement dmStmt = null;
		try {
			dmStmt = dmConn.createStatement();
			
			m_Set = new SimilaritySet(targetTagName, m_SetDimension);
			for(int i = 0; i < m_SetDimension; ++i)
			{
				//	initialize the sets
				
				
				//	read the data into the sets
				String query = "select tag_id_r, sim from sim_tag" + m_SetSequence[i] + " where tag_id_l=" + tagId + " order by sim desc limit " + m_RecordNum;
				
				//	get all the similary tags according to algorithm i
				ResultSet curRs = dmStmt.executeQuery(query);
				
				//	if the name of the tag can be found in dictionary, direct get it,
				//	otherwise, get it from the database and put into the dictionary
				while(curRs.next())
				{
					String tag_R_Id = curRs.getString("tag_id_r");
					String tagRName = getTagNamebyId(tag_R_Id);
					Double sim = curRs.getDouble("sim");
					m_Set.addValueByRecName(tagRName, i, sim);
				}
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return m_Set;
	}

	/**
	 * Get the name of tag by id
	 * @param tagId	the id of the tag
	 * @return	the name of the tag
	 */
	private String getTagNamebyId(String tagId) {
		
		//	exists in dictionary
		if(m_FrequentTagDict.containsKey(tagId))
		{
			return m_FrequentTagDict.get(tagId);
		}
		
		//	read from database
		String targetTagName = null;
		String tagQuery = "select tag from tag_dict where tag_id = " + tagId;
		Connection tagConn = DBConnect.connect(m_TagUrl, m_Usr, m_Pwd);
		try {
			Statement tagStmt = tagConn.createStatement();
			ResultSet tagRs = tagStmt.executeQuery(tagQuery);
			while(tagRs.next())
			{
				targetTagName = tagRs.getString("tag");
			}
			tagConn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//	put into dictionary
		m_FrequentTagDict.put(tagId, targetTagName);
		
		return targetTagName;
	}
	
	
	public SimilaritySet getSimilaritySet()
	{
		return m_Set;
	}
	
	public static void main(String[] args)
	{
		int sequence[] = {1, 2, 3, 4, 5, 6, 7, 8};
		SetReader reader = new SetReader(20, sequence);
		reader.readSets("24");
		SimilaritySet set = reader.getSimilaritySet();
		System.out.println(set);
	}
	
}
